<template>
  <el-form
      ref="formDataRef"
      :model="formData"
      style="width:100%"
      label-position="top">
    <el-row :gutter="10">
      <el-col :span="24">
        <div style="font-size: 16px;font-weight: bold;margin:10px 0px;">您将要重置{{ params.name }}({{
            params.username
          }})的密码:
        </div>
      </el-col>

      <el-col :span="24">
        <el-input v-model="formData.password" type="password"/>
      </el-col>
    </el-row>
  </el-form>
</template>

<script lang="ts" setup>
// @ts-ignore
import {getCurrentInstance, onMounted, reactive, ref} from 'vue'
// @ts-ignore
import {ElMessage, FormInstance, FormRules} from 'element-plus'
//定义组件名称
defineOptions({
  name: "RestPassword"
})

//接收的参数
const props = defineProps({
  params: {required: true, type: Object, default: {}}

});

interface Modal {
  id?: string,
  password?: string
}

const formDataRef = ref<FormInstance>()

// @ts-ignore
const {proxy} = getCurrentInstance()

// 表单 数据
let formData = ref<Modal>({})
//重置事件
const resetForm = () => {
  formData.value = {}
  formDataRef.value?.resetFields();
}


/**
 * 按钮点击事件
 * @param btnType  按钮类型
 * @param params   参数
 * @param rootContext 弹窗父页面
 * @param modelContext  弹窗窗口
 */
const clickEvent = (btnType: string, params: any, rootContext: any, modelContext: any) => {
  switch (btnType) {
    case 'submit':
      let passRegular = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,}$/
      if (formData.value.password && passRegular.test(formData.value.password)) {
        proxy.$http.post('/user-center/user/saveNewPw', {
          password: formData.value.password,
          id: props.params.id
        }).then(function (api: any) {
          if (api.code == 0) {
            rootContext.$refs.defaultTable.reloadTable({});
            ElMessage({
              message: '操作成功',
              type: 'success',
            })
            modelContext.closeModal();
          } else {
            ElMessage({
              message: '操作失败',
              type: 'error',
            })
          }
        });
        console.log('submit!')
      }
      else {
        ElMessage({
          message: '密码必须是不小于8位,包括大小写,数字,特殊字符的组合!',
          type: 'error',
        })
      }
      break;
    case 'reset':
      formData.value = {}
      formDataRef.value?.resetFields();
      break;
  }
}
defineExpose({
  clickEvent
});
</script>
